// pages/fun/webPage/webPage.js
const session = wx.createVKSession({
  track: {
    OCR: { mode: 2 } // mode: 1 - 使用摄像头；2 - 手动传入图像
  },
})
var imageData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    faceImgUrl: '',
    faceImgWidth: 0,
    faceImgHeight: 0,
    faceImgOriginWidth: 0,
    faceImgOriginHeight: 0,
    showPrivacy: false
  },

  /**
   * 生命周期=函数--监听页面加载
   */
  onLoad(options) {
    
    
 
    
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  // 静态图片检测模式下，每调一次 runOCR 接口就会触发一次 updateAnchors 事件
  session.on('updateAnchors', anchors => {
    console.log('anchors.text', "".concat(anchors.map(anchor=>anchor.text)))
  })
  

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.onNeedPrivacyAuthorization(resolve => {
      // 需要用户同意隐私授权时
      // 弹出开发者自定义的隐私授权弹窗
      console.log(resolve)
      this.setData({
        showPrivacy: true
      })
      this.resolvePrivacyAuthorization = resolve
    })
    wx.requirePrivacyAuthorize({
      complete(res){
        console.log(res)
      }
    })
  
  

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  shibiewenzi(){
    console.log(session)
    var that  = this
  
    wx.chooseMedia({
      count: 1,
      sizeType: ['compressed'],
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success(res){
       
        var imgs = res.tempFiles;
        var imgUrl = imgs[0].tempFilePath
        console.log(imgUrl)
          // 需要调用一次 start 以启动

          wx.getImageInfo({
            src: imgUrl,
            success: res => {
                const fixWidth = 300
                const {
                    width,
                    height
                } = res
                console.log('getImageInfo res', res)
                that.setData({
                    faceImgUrl: imgUrl,
                    faceImgWidth: fixWidth,
                    faceImgHeight: (fixWidth / width) * height,
                    faceImgOriginWidth: width,
                    faceImgOriginHeight: height
                })
                that.startOCR()
            },
            fail: res => {
                console.error(res)
            }
        })
       
      },fail(res){
        console.log(res)
      }
    })
  },
  async startOCR(){
    var that = this
    if (this.data.faceImgUrl) {
      const canvas = wx.createOffscreenCanvas({
          type: '2d',
          width: that.data.faceImgOriginWidth,
          height: that.data.faceImgOriginHeight,
      })
      const context = canvas.getContext('2d')
      const img = canvas.createImage()
      await new Promise(resolve => {
          img.onload = resolve
          img.src = that.data.faceImgUrl
      })

      context.clearRect(0, 0, this.data.faceImgOriginWidth, this.data.faceImgOriginHeight)
      context.drawImage(img, 0, 0, this.data.faceImgOriginWidth, this.data.faceImgOriginHeight)

      imageData = context.getImageData(0, 0, this.data.faceImgOriginWidth, this.data.faceImgOriginHeight)

      // console.log('[frameBuffer] --> ', this.imgData.data.buffer)
      // console.log('this.session.runOCR', this.session.runOCR)
      console.log('width', this.data.faceImgOriginWidth)
      console.log('height', this.data.faceImgOriginHeight)

      session.start(errno => {
        console.log(errno)
        if (errno) {
          // 如果失败，将返回 errno
        } else {
          // 否则，返回null，表示成功
          session.runOCR({
            frameBuffer: imageData.data.buffer,
            width: that.data.faceImgOriginWidth,
            height: that.data.faceImgOriginHeight,
          })
        }
      })
      
  }
},
handleAgreePrivacyAuthorization() {
  // 用户点击同意按钮后
  this.resolvePrivacyAuthorization({ buttonId: 'agree-btn', event: 'agree' })
}








  
  
})